﻿using System;
using System.Data.SqlServerCe;

namespace Co9320.NamaAlert.Model
{
	internal partial class LocalDatabaseController
	{
		private class InfoControl
		{
			private static readonly string GetCommandText;
			private static readonly string SetCommandText;

			SqlCeConnection Connection;

			static InfoControl()
			{
				GetCommandText = "SELECT ParamValue FROM SystemInfo WHERE ParamName=@Name;";
				SetCommandText = "UPDATE SystemInfo SET ParamValue=@Value WHERE ParamName=@Name;";
			}

			public InfoControl(SqlCeConnection Connection)
			{
				this.Connection = Connection;
			}

			public int SchemaMajor
			{
				get
				{
					return int.Parse(GetParameter("SchemaMajor"));
				}
			}

			public int SchemaMinor
			{
				get
				{
					return int.Parse(GetParameter("SchemaMinor"));
				}
			}

			public DateTime RecentDeepShrink
			{
				get
				{
					return DateTime.Parse(GetParameter("RecentDeepShrink"));
				}
				set
				{
					SetParameter("RecentDeepShrink", value.ToString());
				}
			}

			public DateTime RecentShallowShrink
			{
				get
				{
					return DateTime.Parse(GetParameter("RecentShallowShrink"));
				}
				set
				{
					SetParameter("RecentShallowShrink", value.ToString());
				}
			}

			private string GetParameter(string ParameterName)
			{

				using (SqlCeCommand cmd = Connection.CreateCommand())
				{
					cmd.CommandText = GetCommandText;
					cmd.Parameters.Add("@Name", ParameterName);

					return (string)cmd.ExecuteScalar();
				}
			}

			private void  SetParameter(string ParameterName, string ParameterValue)
			{
				using (SqlCeCommand cmd = Connection.CreateCommand())
				{
					cmd.CommandText = SetCommandText;
					cmd.Parameters.Add("@Name", ParameterName);
					cmd.Parameters.Add("@Value", ParameterValue);

					cmd.ExecuteNonQuery();
				}
			}

		}

	}
}
